﻿var app = angular.module("app", ["ngRoute"]);
var loadAll = function ($http, $location, PostFactory, ProjectFactory) {

    $location.path("/");
};


app.config(function ($routeProvider) {
    $routeProvider.when("/", {
        template: " ",
        controller: "loadPartial"
    }).when("/AddNewPost/",
    {
        templateUrl: "/App/Views/ngAddNewPost.html",
        controller: "ngAddNewPostController",
    }).when("/Index", {
        templateUrl: "/App/Views/ngIndex.html",
        controller: "ngHomeController"
    })
});

app.factory("PostFactory", function () {
    return [];
});

app.factory("ProjectFactory", function () {
    return [];
});
//app.factory("PaginationFactory", function (PostFactory) {
//    return {
//        currentPage: 0,
//        pageSize: 2,
//        numberOfPages: function (PostFactory, pageSize) {
//            return Math.ceil(PostFactory.length / pageSize);
//        }
//    }
//});
app.controller("loadPartial", function ($scope, $http, $location, PostFactory, ProjectFactory,$q) {
    //get partial posts
    var getInit = function () {
        var deferred = $q.defer();
        $scope.test = PostFactory;
        console.log("before initial get");
        $http.get("/api/ngPosts/" + 0 + "/" + 2).success(function (data) {
            console.log("inside get in initial get");
            for (var x in data) {
                console.log(data[x]);
                $scope.test.push(data[x]);
                deferred.resolve();
            }
        }).error(function (data) {
            console.log((data));
            deferred.reject();
        });
        return deferred.promise;
    }
    getInit().then(function () { $location.path("/Index") });
    //get random project
    console.log("inside loadpartial ctrlr")
    console.log($scope.test.length);
});

app.controller("ngHomeController", function ngHomeController($scope, $http, $location, PostFactory, ProjectFactory, $routeParams, $q){
    $scope.featured = "";
    console.log("inside home ctrlr");
    console.log(PostFactory);
    //console.log(ProjectFactory);
    $scope.posts = PostFactory;

    var deferred = $q.defer();
    var x = (Math.floor((Math.random() * 5) + 1));
    var project;
    $http.get("/api/ngProjects/" + x).success(function (data) {
        $scope.featured = data;
        deferred.resolve();
    }).error(function (data) {
        console.log((data));
    });
    return deferred.promise;

    console.log($scope.posts.length);
    //get next two posts
    $http.get("/api/ngPosts/" + ($scope.posts.length-1) + "/" + 2).success(function (data) {
        for (var x in data) {
            console.log(data[x]);
            $scope.posts.push(data[x]);
        }
    }).error(function (data) {
        console.log((data));
    });

    //get all Posts
    //$http.get("/api/ngPosts/").success(function (data) {
    //    for (var x in data) {
    //        PostFactory.push(data[x]);
    //    }
    //}).error(function (data) {
    //    console.log((data));
    //});
    //var x = ProjectFactory.length;
    //$scope.featured = ProjectFactory[Math.floor((Math.random() * x) + 1)];

    //get all projects
    //$scope.projects = ProjectFactory;
    //$http.get("/api/ngProjects/").success(function (data) {
    //    for (var x in data) {
    //        ProjectFactory.push(data[x]);
    //    }

    //}).error(function (data) {
    //    console.log((data));
    //});
    $scope.currentPage = 0;
    $scope.pageSize = 2;
    $scope.numberOfPages = function () {
        return Math.ceil($scope.posts.length / $scope.pageSize);
    }
    $scope.AddNewPost = function () {
        $location.path("AddNewPost/");
    }
});

app.controller("ngAddNewPostController", function ($scope, $location, $http, PostFactory) {

    $scope.SubmitNewPost = function (post) {
        $http.post("/api/ngPosts", post).success(function (data) {
            post = data;
            post.Id = data.Id;
            PostFactory.push(post);
            $location.path("/");
        }).error(function () {
            console.log(data);
            $location.path("/");
        });
    };
});

//startFrom filter
app.filter('startFrom', function () {
    return function (input, start) {
        start = +start; //parse to int
        return input.slice(start);
    }
});